/*
 * PageSetupDialog.cs - Wordtrainer
 * 
 * Copyright (C) 1998-2007 Markus Kilås
 *               
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 *
 * Authors:
 *   Markus Kilås <markus@markuspage.com>
 * 
 */

using System;
using Gtk;

namespace WtSharp
{
	
	
	public partial class PageSetupDialog : Gtk.Dialog
	{
		private PageSettings m_PageSettings;
		
		public PageSetupDialog(Gtk.Window parent, PageSettings pageSettings, string printTitle) : base("Find", parent, Gtk.DialogFlags.Modal)
		{
			this.Build();
			entryTitle.Text = printTitle;
			
			LoadSettings(pageSettings);
		}
		
		private void LoadSettings(PageSettings settings)
		{
			this.m_PageSettings = settings;
			
			checkbuttonTitle.Active = m_PageSettings.PrintTitle;
			checkbuttonPageNumbers.Active = m_PageSettings.PrintPageNumber;
			
			switch(m_PageSettings.PrintWords)
			{
			case PageSettings.PrintWordsType.OnlyF:
				radiobuttonWordsOnlyF.Active = true;
				break;
			case PageSettings.PrintWordsType.OnlyT:
				radiobuttonWordsOnlyT.Active = true;
				break;
			default:
				radiobuttonWordsBoth.Active = true;
				break;
			}
			
			entryCharacterSize.Text = ""+m_PageSettings.FontSize;
			
			entryMarginTop.Text = ""+m_PageSettings.MarginTop;
			entryMarginBottom.Text = ""+m_PageSettings.MarginBottom;
			entryMarginLeft.Text = ""+m_PageSettings.MarginLeft;
			entryMarginRight.Text = ""+m_PageSettings.MarginRight;
		}
		
		public string PrintTitle
		{
			get
			{
				return entryTitle.Text;
			}
		}
		
		private void CheckEntryIntValue(Entry entry, string error, string standard, int minValue)
		{
			try 
			{
				int n = Int32.Parse(entry.Text);
				if(n < minValue)
					throw new FormatException();
			}
			catch(FormatException)
			{
				MessageDialog md = new MessageDialog (this, 
	                                      DialogFlags.DestroyWithParent,
				                          MessageType.Error, 
	                                      ButtonsType.Ok, error);
				md.Run();
				md.Destroy();
				entry.Text = standard;
				entry.GrabFocus();
			}
		}

		protected virtual void OnButtonOkClicked (object sender, System.EventArgs e)
		{
			m_PageSettings.PrintTitle = checkbuttonTitle.Active;
			m_PageSettings.PrintPageNumber = checkbuttonPageNumbers.Active;
			
			if(radiobuttonWordsOnlyF.Active)
				m_PageSettings.PrintWords = PageSettings.PrintWordsType.OnlyF;
			else if(radiobuttonWordsOnlyT.Active)
				m_PageSettings.PrintWords = PageSettings.PrintWordsType.OnlyT;
			else
				m_PageSettings.PrintWords = PageSettings.PrintWordsType.Both;
			
			
			m_PageSettings.FontSize = Int32.Parse(entryCharacterSize.Text);
			
			m_PageSettings.MarginTop = Int32.Parse(entryMarginTop.Text);
			m_PageSettings.MarginBottom = Int32.Parse(entryMarginBottom.Text);
			m_PageSettings.MarginLeft = Int32.Parse(entryMarginLeft.Text);
			m_PageSettings.MarginRight = Int32.Parse(entryMarginRight.Text);
		
			Destroy();
		}
			
		protected virtual void OnEntryCharacterSizeFocusOutEvent (object o, Gtk.FocusOutEventArgs args)
		{	
			CheckEntryIntValue(entryCharacterSize, "Incorrect font size", "12", 1);
		}

		protected virtual void OnEntryMarginTopFocusOutEvent (object o, Gtk.FocusOutEventArgs args)
		{
			CheckEntryIntValue(entryMarginTop, "Incorrect margin value", "0", 0);
		}

		protected virtual void OnEntryMarginBottomFocusOutEvent (object o, Gtk.FocusOutEventArgs args)
		{
			CheckEntryIntValue(entryMarginBottom, "Incorrect margin value", "0", 0);
		}

		protected virtual void OnEntryMarginLeftFocusOutEvent (object o, Gtk.FocusOutEventArgs args)
		{
			CheckEntryIntValue(entryMarginLeft, "Incorrect margin value", "0", 0);
		}

		protected virtual void OnEntryMarginRightFocusOutEvent (object o, Gtk.FocusOutEventArgs args)
		{
			CheckEntryIntValue(entryMarginRight, "Incorrect margin value", "0", 0);
		}
			
		protected virtual void OnButtonCancelClicked (object sender, System.EventArgs e)
		{
			Destroy();
		}
		
	}
}
